<?php
class Module {
	public $id;
	public $name;
	public $path;
	public $logo;
	public $admin;
	public $droits;

	public function __construct($id,$name,$path,$logo,$admin,$droits) {
		$this->id = $id;
		$this->name = $name;
		$this->path = $path;
		$this->logo = $logo;
		$this->admin = $admin;
		$this->droits = $droits;
	}

	public static function GetListModules($admin = false) {
		global $debug_var;
		$adminSQL = ($admin) ? "true" : 'false';
		$query = "SELECT * from modules WHERE admin = $adminSQL";
		$results = DBselect($query);
		$modules = array();
		foreach ($results as $res){
			$modules[$res["id"]] = new Module($res["id"],$res["name"],$res["path"],$res["logo"],$res["admin"],null);
			if($admin) {
				$modules[$res["id"]]->getDroits();
			}
			if(DEBUG) {
				$debug_var["Modules"][$res["id"]] = array("Nom"=>$modules[$res["id"]]->name,
													 "droits"=>$modules[$res["id"]]->droits
													);
			}
		}
		return $modules;
	}

	public function getDroits() {
		$query = "SELECT g_id FROM droits_module WHERE m_id = ".$this->id;
		$results = DBselect($query);
		foreach ($results as $res){
			$this->droits[$res["g_id"]] = $res["g_id"];
		}
	}

	public static function IsModuleOK($name,$isAdmin) {
		$adminSQL = ($isAdmin) ? "true" : 'false';
		$query= "SELECT id, count(*) as nb from modules where path = '$name' AND admin = $adminSQL";
		$result = DBselect($query);
		if ($result[0]["nb"]==1) {
			return $result[0]["id"];
		}
		else {
			return false;
		}
	}
}
?>